﻿using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using ECP.Model;
using ECP.Service.Utilities;
using ECP.Utilities;
using ECP.Utilities.Controls;

namespace ECP.Client.Module
{
    public partial class ModuleSelectFrm : FormBase, IBaseSelectModuleForm
    {
        private bool allowNull;
        private bool allowSelect;

        private bool checkMove;

        private string currentEntityID;
        private DataTable DTModule;
        private bool multiSelect;
        private string openID;
        private string selectedCode;
        private string selectedFullName;
        private string selectedID;

        public delegate bool ButtonConfirmEventHandler();
        public ModuleSelectFrm()
        {
            this.DTModule = new DataTable(BaseModuleTable.TableName);
            this.allowSelect = true;
            this.selectedID = string.Empty;
            this.selectedCode = string.Empty;
            this.selectedFullName = string.Empty;
            this.openID = string.Empty;
            this.currentEntityID = string.Empty;
            this.InitializeComponent();
        }

        public ModuleSelectFrm(string currentID)
            : this()
        {
            this.CurrentEntityID = currentID;
            this.OpenID = currentID;
        }

        public ModuleSelectFrm(string currentID, string parentID)
            : this()
        {
            this.OpenID = currentID;
            this.CurrentEntityID = parentID;
        }

        private void BindData(bool reloadTree)
        {
            if (reloadTree)
            {
                this.LoadTree();
            }
            if ((this.tvModule.FocusedNode == null) && (this.tvModule.Nodes.Count > 0))
            {
                if (this.CurrentEntityID.Length == 0)
                {
                    this.tvModule.FocusedNode = this.tvModule.Nodes[0];
                }
                else
                {
                    BaseInterfaceLogic.FindTreeNode(this.tvModule, this.CurrentEntityID, this.colParentID);
                    if (BaseInterfaceLogic.TargetNode != null)
                    {
                        this.tvModule.FocusedNode = BaseInterfaceLogic.TargetNode;
                        BaseInterfaceLogic.ExpandTreeNode(this.tvModule);
                    }
                }
                if (this.tvModule.FocusedNode != null)
                {
                    this.tvModule.FocusedNode.Expanded = true;
                    this.tvModule.FocusedNode.Selected = true;
                }
            }
        }

        private bool CheckInput()
        {
            bool flag = false;
            if (this.tvModule.FocusedNode != null)
            {
                flag = true;
            }
            if (!flag)
            {
                MessageBox.Show(AppMessage.MSG0023, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return flag;
        }

        private bool CheckInputMove()
        {
            bool flag = true;
            BaseInterfaceLogic.FindTreeNode(this.tvModule, this.OpenID, this.colParentID);
            TreeListNode targetNode = BaseInterfaceLogic.TargetNode;
            BaseInterfaceLogic.FindTreeNode(this.tvModule, this.CurrentEntityID, colParentID);
            TreeListNode targetTreeNode = BaseInterfaceLogic.TargetNode;
            if (!BaseInterfaceLogic.TreeNodeCanMoveTo(targetNode, targetTreeNode,colTreelist))
            {
                MessageBox.Show(AppMessage.Format(AppMessage.MSG0036, new string[] { targetNode.GetValue(colTreelist).ToString(), targetTreeNode.GetValue(colTreelist).ToString() }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                flag = false;
            }
            return flag;
        }

    
        private void FormOnLoad()
        {
            base.Localization(this);
            this.DTModule = ServiceManager.Instance.ModuleService.GetDT(base.UserInfo);
         //   BaseBusinessLogic.SetFilter(this.DTModule, BaseModuleTable.FieldEnabled, "1");
            this.DTModule.AcceptChanges();
            this.BindData(true);
            this.SetControlState();
        }

     
        private void LoadTree()
        {
            this.tvModule.BeginUpdate();
            this.tvModule.Nodes.Clear();
             BaseInterfaceLogic.LoadTreeNodes(this.DTModule, BaseModuleTable.FieldID, BaseModuleTable.FieldParentID, BaseModuleTable.FieldFullName, this.tvModule);
   
            BaseInterfaceLogic.FindTreeNode(this.tvModule, this.OpenID, this.colParentID);
            if (BaseInterfaceLogic.TargetNode != null)
            {
                this.tvModule.FocusedNode = BaseInterfaceLogic.TargetNode;
                BaseInterfaceLogic.ExpandTreeNode(this.tvModule);
                this.tvModule.FocusedNode.Selected = true;
                this.tvModule.FocusedNode.Expanded = true;
            }
            this.tvModule.EndUpdate();
        }

      

        private void SetControlState()
        {
            this.btnSetNull.Visible = this.AllowNull;
            if (this.DTModule.Rows.Count == 0)
            {
                this.btnConfirm.Enabled = false;
            }
            else
            {
                this.btnConfirm.Enabled = true;
            }
        }

        public bool AllowNull
        {
            get
            {
                return this.allowNull;
            }
            set
            {
                this.allowNull = value;
            }
        }

        public bool AllowSelect
        {
            get
            {
                return this.allowSelect;
            }
            set
            {
                this.allowSelect = value;
                this.SetControlState();
            }
        }

        public bool CheckMove
        {
            get
            {
                return this.checkMove;
            }
            set
            {
                this.checkMove = value;
            }
        }

        public string CurrentEntityID
        {
            get
            {
                if ((this.tvModule.FocusedNode != null) && (this.tvModule.FocusedNode.GetValue(colParentID) != null))
                {
                    this.currentEntityID = this.tvModule.FocusedNode.GetValue(colParentID).ToString();
                }
                return this.currentEntityID;
            }
            set
            {
                this.currentEntityID = value;
            }
        }

        public bool MultiSelect
        {
            get
            {
                return this.multiSelect;
            }
            set
            {
                this.multiSelect = value;
            }
        }

        public string OpenID
        {
            get
            {
                return this.openID;
            }
            set
            {
                this.openID = value;
            }
        }

        public string SelectedCode
        {
            get
            {
                return this.selectedCode;
            }
            set
            {
                this.selectedCode = value;
            }
        }

        public string SelectedFullName
        {
            get
            {
                return this.selectedFullName;
            }
            set
            {
                this.selectedFullName = value;
            }
        }

        public string SelectedID
        {
            get
            {
                return this.selectedID;
            }
            set
            {
                this.selectedID = value;
            }
        }


        public event ButtonConfirmEventHandler OnButtonConfirmClick;

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            base.Close();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                this.SelectedID = this.tvModule.FocusedNode.GetValue(this.colParentID).ToString();
                this.SelectedCode = BaseBusinessLogic.GetProperty(this.DTModule, this.SelectedID, BaseModuleTable.FieldCode);
                this.SelectedFullName = this.tvModule.FocusedNode.GetValue(this.colTreelist).ToString();
                if ((!this.CheckMove || this.CheckInputMove()) && ((this.OnButtonConfirmClick == null) || this.OnButtonConfirmClick()))
                {
                    base.DialogResult = System.Windows.Forms.DialogResult.OK;
                    base.Close();
                }
            }
        }

        private void btnSetNull_Click(object sender, EventArgs e)
        {
            this.SelectedID = string.Empty;
            this.SelectedFullName = string.Empty;
            base.DialogResult = System.Windows.Forms.DialogResult.OK;
            base.Close();
        }

        private void tvModule_DoubleClick(object sender, EventArgs e)
        {
            this.btnConfirm.PerformClick();
        }

        private void ModuleSelectFrm_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                base.FormLoaded = false;
                this.FormOnLoad();
                base.FormLoaded = true;
            }
            catch (Exception exception)
            {
                base.ProcessException(exception);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void ModuleSelectFrm_KeyDown(object sender, KeyEventArgs e)
        {
            int keyValue = e.KeyValue;
            if ((keyValue != 0x2e) && (keyValue == 0x74))
            {
                this.FormOnLoad();
            }
        }

        private void tvModule_NodeChanged(object sender, DevExpress.XtraTreeList.NodeChangedEventArgs e)
        {
              if (e.Node.HasChildren)
            {
                e.Node.ImageIndex = 1;
                e.Node.SelectImageIndex = 1;
            }
            else
            {
                e.Node.SelectImageIndex = 2;
            }
        }

      
    }
}